var ox=0;
var foodsdata;
var needfoods;
$(document).ready(function(){
	lookAllPosition();
	$("#new-handle").on('click',function(){
		var positionid = sessionStorage.positionid;
		if(positionid>0){
			closePsition(positionid);
		}
		/*var x=$(".desk-num").html();
		$("#desk-"+x).attr("class","desk-yes");*/
	});
	$("#des").on('click','.desk div',function(){
		var positionid=$(this).attr("positionid");
		sessionStorage.positionid = positionid;
		sessionStorage.positionname = $(this).text();
		var positionname=$(this).text();
		var i=$(this).attr("class").split("-")[1];
		$(".desk-num").html(positionname);
		if (i=='new') {
			$("#menu").hide();
			$("#new-menu").show();
			$(".desk-state").html("有人");
			seeMenus(positionid);
		}else if(i=='yes'){
			$("#new-menu").hide();
			$("#menu").show();
			$(".desk-state").html("有人");
			seeOrder(positionid);
		}else if(i=='no'){
			$("#new-menu").hide();
			$("#menu").hide();
			$(".desk-state").html("无人");
		}
	});
	$("#head button").click(function(){
		var r=confirm("你确定要退出登录！");
		if (r==true){
			logout();
		}
	});
	$(".radioo").click(function(){
		$("#three-submit").removeAttr("disabled");
		$("#people").slideUp();
		$("#referee").slideUp();
		ox=0;
	});
	$(".radiox").click(function(){
		$("#three-submit").attr("disabled", true);
		$("#people").slideUp();
		$("#referee").slideDown();
		ox=1;
	});
	$(".radioc").click(function(){
		$("#three-submit").attr("disabled", true);
		$("#referee").slideUp();
		$("#people").slideDown();
		ox=2;
	});
	$("#foods").on('click', '.reduce',function(){
		var positionid = sessionStorage.positionid;
		var orderid = $(this).attr("name");
		var num = prompt("输入退回数量:","1");
		if(parseInt(num)==num&&num!=null){
			reduceFood(positionid,orderid,num);
		}
	});
	$("#btn-alt1").click(function(){//验证推荐人（有推荐人的情况下）
		$.ajax({
			type:"get",
			url:"/ldss/customers/validate",
			async:false,
			data:{
				tel:$("*[name='three-oldphone']").val(),
			},
			dataType:"json",
			success: function (data) {
				if(data.success){
					sessionStorage.refereeId = data.message;
					$("#three-submit").removeAttr("disabled");
					$("#alt-red").hide();
				}
				else{
					$("#alt-red").show();
					$("#three-submit").attr("disabled", true);
					alert("此人还未出生！")
				}
			},
			error: function(){
				alert("请求出错");
			}
		})
	});
	$("#btn-alt2").click(function(){//验证推荐人（让自己成为推荐人）
		$.ajax({
			type:"get",
			url:"/ldss/customers/validate",
			async:false,
			data:{
				tel:$("*[name='three-oldphone']").val(),
			},
			dataType:"json",
			success: function (data) {
				if(data.success){
					sessionStorage.refereeId = data.message;
					$("#three-submit").removeAttr("disabled");
					$(".new_name").removeAttr("disabled");
					$(".new_tel").removeAttr("disabled");
					$(".new_pwd").removeAttr("disabled");
					$(".new_rpwd").removeAttr("disabled");
					$("#alt-red").hide();
				}
				else{
					$("#alt-red").show();
					$("#three-submit").attr("disabled", true);
					$(".new_name").attr("disabled", true);
					$(".new_tel").attr("disabled", true);
					$(".new_pwd").attr("disabled", true);
					$(".new_rpwd").attr("disabled", true);
					alert("此人还未出生！")
				}
			},
			error: function(){
				alert("请求出错");
			}
		})
	});
	$("#flow").click(function(){//留帐
		alert("暂时不能留帐")
	});
	$("#three-submit").click(function(){//结账
		var positionid = sessionStorage.positionid;
		if(ox==0){
			closeTNoRefree(positionid);
		}else if(ox==1){
			refereetel = $("*[name='three-oldphone']").val();
			closeTWithRefree(positionid,refereetel);
		}else if(ox==2){
			refereetel = $("*[name='three-oldphone']").val();
			if(refereetel==""||refereetel==null){
				alert("请输入推荐人手机号码");
				return;
			}
			var new_name = $("*[name='three-name']").val();
			if(new_name==""||new_name==null){
				alert("请输入顾客名称");
				return;
			}
			var new_tel = $("*[name='three-phone']").val();
			if(new_tel==""||new_tel==null){
				alert("请输入顾客手机号码");
				return;
			}
			var new_pwd = $("*[name='three-password']").val();
			if(new_pwd==""||new_pwd==null){
				alert("请输入密码");
				return;
			}
			var new_rpwd = $("*[name='three-bpassword']").val();
			if(new_rpwd!=new_pwd){
				alert("两次输入密码不同");
				return;
			}
			closeTAndAddRefree(positionid,refereetel,new_name,new_tel,new_pwd);
		}
	});
	setInterval('lookAllPosition()',5000);//启动定时刷新
});

function closePsition(positionid){//菜上全(处理按钮)
	$.ajax({
		type:"get",
		url:"/ldss/orders/closepsition",
		async:false,
		data:{
			positionid:positionid
		},
		dataType:"json",
		success: function (data) {
			var data = data;
			if(data.success){
				$("#new-menu").hide();
				printBil2();
			}else{
				alert(data.message);
			}
		}
	});
}
function closeTNoRefree(positionid){//结账(没有推荐人)
	$.ajax({
		type:"get",
		url:"/ldss/transaction/closetno",
		async:false,
		data:{
				positionid:positionid
			},
		dataType:"json",
		success: function (data) {
			if(data.success){
				var data = data;
				$("#people").slideUp();
				$("#referee").slideUp();
				ox=0;
				$("#menu").hide();
				printBill(data);
			}else{
				alert(data.message);
			}
		},
		error: function(){
			alert("请求出错");
		}
	});
}
function closeTWithRefree(positionid,refereetel){//结账(有推荐人)
	$.ajax({
		type:"get",
		url:"/ldss/transaction/closetyes",
		async:false,
		data:{
				positionid:positionid,
				refereetel:refereetel
			},
		dataType:"json",
		success: function (data) {
			if(data.success){
				var data = data;
				$("#people").slideUp();
				$("#referee").slideUp();
				ox=0;
				$("#menu").hide();
				printBill(data);
			}else{
				alert(data.message);
			}
		},
		error: function(){
			alert("请求出错");
		}
	});
}
function closeTAndAddRefree(positionid,refereetel,new_name,new_tel,new_pwd){//结账(同时录入新推荐人信息)
	$.ajax({
		type:"get",
		url:"/ldss/transaction/closetadd",
		async:false,
		data:{
				positionid:positionid,
				refereetel:refereetel,
				newname:new_name,
				newtel:new_tel,
				newpsswd:new_pwd
			},
		dataType:"json",
		success: function (data) {
			if(data.success){
				var data = data;
				var positionid = $("#positions").val();
				$("#people").slideUp();
				$("#referee").slideUp();
				ox=0;
				$("#menu").hide();
				printBill(data);
			}else{
				alert(data.message);
			}
		},
		error: function(){
			alert("请求出错");
		}
	});
}
function seeOrder(positionid){//查看这个位置点了什么菜（用于结账）
	$.ajax({
		type:"get",
		url:"/ldss/orders/showmenu",
		async:false,
		data:{
				positionid:positionid
			},
		dataType:"json",
		success: function (data) {
			if(data.success){
				var data = data.data;
				foodsdata = data;
				$("#foods").empty();
				var allprise = 0;
				for(var i = 0; i < data.length; i++){
					var everpeist = data[i].num*data[i].menuprise;
					allprise = allprise+everpeist;
					$("#foods").append(
						"<tr>"+
							"<td>"+data[i].menuname+"</td>"+
							"<td>"+data[i].menuprise+"</td>"+
							"<td>"+data[i].num+"</td>"+
							"<td>"+everpeist+"</td>"+
							"<td>"+data[i].ordertype+"</td>"+
							"<td>"+ 
								"<button type='button' class='btn btn-primary reduce' name='"+data[i].id+"'退回</button>"+
							"</td>"+
						"</tr>"
					);
				}
				$("#foods").append(
					"<tr>"+
						"<td colspan='3'>总价</td>"+
						"<td colspan='3'>"+allprise+"</td>"+
					"</tr>"
				);
			}
		},
		error:function(){
			alert("请求数据失败");
		}
	});
}
function printBill(closedata){//结账时打印小票
	var closedata = closedata.data;
	$("#billnum").empty();
	$("#billnum").append(closedata.billnum);
	$("#billtime").empty();
	$("#billtime").append(closedata.billtime);
	$("#billstore").empty();
	$("#billstore").append(closedata.billstore);
	$("#billposition").empty();
	$("#billposition").append(closedata.billposition);
	$("#ishaverefree").empty();
	$("#ishaverefree").append(closedata.ishaverefree);
	$("#isberefree").empty();
	$("#isberefree").append(closedata.isberefree);
	
	$("#billfoods").empty();
	var allprise = 0;
	for(var i = 0; i < foodsdata.length; i++){
		var everpeist = foodsdata[i].num*foodsdata[i].menuprise;
		allprise = allprise+everpeist;
		$("#billfoods").append(
			"<li style='margin-right: 10px'>" +
			foodsdata[i].menuname+"&nbsp&nbsp&nbsp"+foodsdata[i].num+"&nbsp&nbsp&nbsp"+everpeist+
			"</li>"
		);
	}
	$("#allprise").empty();
	$("#allprise").append(allprise);
	
	var LODOP = getLodop();
	LODOP.PRINT_INIT("结账小票打印");
	LODOP.ADD_PRINT_HTM(20,10,"100%","100%",$("#bill1").html());
	LODOP.PRINTA();//选择打印机
	/*LODOP.PRINT();//直接打印
	LODOP.PREVIEW();//打印预览
*/	
	
	
	/*LODOP.ADD_PRINT_RECT(10,55,360,220,0,1);//打印一个矩形框
	LODOP.SET_PRINT_STYLE("FontSize",11);//字体大小
	LODOP.ADD_PRINT_TEXT(20,180,100,25,"家和铁锅炖");//顶端px、距左边px、宽px、高px、内容为“”
	
	//打印一条直线intShapeType,intTop,intLeft,intWidth,intHeight,intLineStyle,intLineWidth,intColor
	LODOP.ADD_PRINT_SHAPE(1,45,2,500,1,0,1,"#800000");
	
	//2是xx栏的增加顺序号（序号设0表示最新对象）,"FontName"和"FontSize"表示设置字体名和字体大小。
	LODOP.SET_PRINT_STYLEA(2,"FontName","隶书");
	LODOP.SET_PRINT_STYLEA(2,"FontSize",15);
	
	LODOP.ADD_PRINT_TEXT(53,187,75,20,"科学家");
	LODOP.PRINT();//直接打印
	LODOP.PREVIEW();//打印预览
*/}
function printBil2(){//处理订单时打印小票
	var positionname = sessionStorage.positionname;
	$("#needposition").empty();
	$("#needposition").append(positionname);
	
	$("#needfoods").empty();
	for(var i = 0; i < needfoods.length; i++){
		$("#needfoods").append(
			"<li style='margin-right: 10px'>" +
			needfoods[i].menuname+"&nbsp&nbsp&nbsp"+needfoods[i].num+
			"</li>"
		);
	}
	
	var LODOP = getLodop();
	LODOP.PRINT_INIT("下单小票打印");
	LODOP.ADD_PRINT_HTM(20,10,"100%","100%",$("#bill2").html());	
	LODOP.PREVIEW();//打印预览
	/*LODOP.PRINT();//直接打印
*/}
function reduceFood(positionid,orderid,num){//退菜
	$.ajax({
		type:"get",
		url:"/ldss/orders/reduce",
		async:false,
		data:{
				orderid:orderid,
				num:num
			},
		dataType:"json",
		success: function (data) {
			if(data.success){
				var data = data;
				seeOrder(positionid);
			}else{
				alert(data.message);
			}
		},
		error: function(){
			alert("请求出错");
		}
	});
}
function seeMenus(positionid){//查看这个位置有什么需求
	$.ajax({
		type:"get",
		url:"/ldss/orders/showhelpmenu",
		/*async:false,*/
		data:{
			positionid:positionid
		},
		dataType:"json",
		success: function (data) {
			if(data.success){
				var data = data.data;
				needfoods = data;
				$("#addmenu").empty();
				if(data.length>0){
					for(var i = 0; i < data.length; i++){
						$("#addmenu").append(
							"<tr>"+
							"<td>"+data[i].menuname+"</td>"+
							"<td>"+data[i].num+"</td>"+
							"<td>"+data[i].ordertype+"</td>"+
							"</tr>"
							);
					}
				}else{
					$("#addmenu").append("暂无需求");
				}
				
			}else{
				alert(data.message);
			}
		}
	});
}
function lookAllPosition(){//查看所有的位置
	$.ajax({
		type:"get",
		url:"/ldss/orders/lookallposition",
		dataType:"json",
		/*async:false,*/
		success: function (data) {
			if(data.success){
				var data = data.data;
				$("#des").empty();
				for(var i = 0; i < data.length; i++){
					if(data[i].flag==0){
						$("#des").append(//无人
								"<div class='desk col-lg-2'><div class='desk-no' positionid='"+
								data[i].id+
								"'>"+data[i].positionName+"</div></div>");
					}else if(data[i].flag==1){//有人
						$("#des").append(
								"<div class='desk col-lg-2'><div class='desk-yes' positionid='"+
								data[i].id+
								"'>"+data[i].positionName+"</div></div>");
					}else if(data[i].flag==2){//有加菜
						$("#des").append(
								"<div class='desk col-lg-2'><div class='desk-new' positionid='"+
								data[i].id+
								"'>"+data[i].positionName+"</div></div>");
					}
				}
			}else{
				alert(data.message);
			}
		},
		error: function(){
			alert("请求出错");
		}
	});
}
/*function logout(){
	$.ajax({
		type:"get",
		url:"/ldss/user/logout",
		dataType:"json",
		async:false,
		success: function (data) {
			if(data.success){
				window.location.href=("/ldss/ldss/login");
			}
		},
		error: function(){
			alert("请求出错");
		}
	});
}*/